package lj.courseinterect.dao;

import lj.courseinterect.dao.i.CourseInfoDao;
import lj.courseinterect.dao.i.QuestionInfoDao;
import lj.courseinterect.entity.CourseInfo;
import lj.courseinterect.entity.QuestionInfo;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class JpaQuestionInfoDao extends JpaDao<QuestionInfo, Long> implements QuestionInfoDao{

    public JpaQuestionInfoDao() {
        super(QuestionInfo.class);
    }


    
    @Override
    @Transactional(readOnly = true)

    public List<QuestionInfo> findByMajorId(Long majorId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<QuestionInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<QuestionInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("majorId");
        criteriaQuery.where(builder.equal(namePath, majorId));


        TypedQuery<QuestionInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<QuestionInfo> questionInfos = typedQuery.getResultList();

        if (questionInfos.isEmpty()) {
            return null;
        }
        return questionInfos;
    }

    @Override
    public List<QuestionInfo> findByStuId(Long stuId) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<QuestionInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<QuestionInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("stuId");
        criteriaQuery.where(builder.equal(namePath, stuId));


        TypedQuery<QuestionInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        List<QuestionInfo> questionInfos = typedQuery.getResultList();

        if (questionInfos.isEmpty()) {
            return null;
        }
        return questionInfos;
    }

    @Override
    public List<QuestionInfo> findByCouIdOrderByTime(Long couId, int pageNum, int pageCount) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<QuestionInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<QuestionInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("couId");
        criteriaQuery.where(builder.equal(namePath, couId)).orderBy(builder.desc(root.get("queTime")));

        TypedQuery<QuestionInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        typedQuery.setFirstResult(pageNum * pageCount);
        typedQuery.setMaxResults(pageCount);
        List<QuestionInfo> questionInfos = typedQuery.getResultList();

        if (questionInfos.isEmpty()) {
            return null;
        }
        return questionInfos;
    }

    @Override
    public List<QuestionInfo> findByCouIdOrderByGood(Long couId, int pageNum, int pageCount) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<QuestionInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<QuestionInfo> root = criteriaQuery.from(this.entityClass);
        Path<String> namePath = root.get("couId");
        criteriaQuery.where(builder.equal(namePath, couId)).orderBy(builder.desc(root.get("goodCount")));

        TypedQuery<QuestionInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        typedQuery.setFirstResult(pageNum * pageCount);
        typedQuery.setMaxResults(pageCount);
        List<QuestionInfo> questionInfos = typedQuery.getResultList();

        if (questionInfos.isEmpty()) {
            return null;
        }
        return questionInfos;
    }

    @Override
    public List<QuestionInfo> findAllOrderByGood(int pageNum, int pageCount) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<QuestionInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<QuestionInfo> root = criteriaQuery.from(this.entityClass);
        criteriaQuery.orderBy(builder.desc(root.get("goodCount")));

        TypedQuery<QuestionInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        typedQuery.setFirstResult(pageNum * pageCount);
        typedQuery.setMaxResults(pageCount);
        List<QuestionInfo> questionInfos = typedQuery.getResultList();

        if (questionInfos.isEmpty()) {
            return null;
        }
        return questionInfos;
    }

    @Override
    public List<QuestionInfo> findAllOrderByTime(int pageNum, int pageCount) {
        final CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
        final CriteriaQuery<QuestionInfo> criteriaQuery = builder.createQuery(this.entityClass);

        Root<QuestionInfo> root = criteriaQuery.from(this.entityClass);
        criteriaQuery.orderBy(builder.desc(root.get("queTime")));

        TypedQuery<QuestionInfo> typedQuery = this.getEntityManager().createQuery(criteriaQuery);
        typedQuery.setFirstResult(pageNum * pageCount);
        typedQuery.setMaxResults(pageCount);
        List<QuestionInfo> questionInfos = typedQuery.getResultList();

        if (questionInfos.isEmpty()) {
            return null;
        }
        return questionInfos;
    }

    @Override
    public List querySelectedCourseByStuId(Long stuId, int pageNum, int pageCount){
        String queryQuestion = "select q.queId, s.stuId, sc.scId from QuestionInfo q, Student s, StudentCourse sc where s.stuId = :stuId and s.stuId = sc.stuId and sc.couId = q.couId order by q.goodCount desc";

        Query query = this.getEntityManager().createQuery(queryQuestion);
        query.setParameter("stuId", stuId);
        query.setFirstResult(pageNum * pageCount);
        query.setMaxResults(pageCount);
        List ids = query.getResultList();

        if (ids.isEmpty()) {
            return null;
        }
        return ids;
    }
}
